<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单校验</title>

</head>
<body>
    <form id="regist" method="get" onsubmit="">
        用户名:<input type="text" id="username" name="username" value=""><br>
        密码:<input type="password" id="password" name="password" value=""><br>
        <input type="submit" id="submit" name="注册"><br>
    </form>
</body>
    <script>
        document.getElementById("regist").onsubmit=function () {
            let username=document.getElementById("username").value;
            let password=document.getElementById("password").value;

            let reg1=/^[a-zA-Z{4,16}]$/;
            if (!reg1.test(username)){
                alert("用户名不符合规则");
                return false;
            }
            let reg2=/^[\d{6}]$/;
            if (!reg2.test(password)){
                alert("密码不符合规则");
                return false;
            }
            return true;
        }
    </script>
</html>